#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include "rngs.h"
#include "interface.h"

int numerrors;

int randInt(int low, int high)
{
	int temp = low + rand() % (high -low +1);
	return temp;

}



void testActionPhase(int player, struct gameState* state){
	int i;
	int card;
	char name[100];
	int isAction;
	int count;
	int deckcount;

	count = state->numActions;

	while (count > 0){
	   for (i = 0; i < numHandCards(state); i++) {
	    card = handCard(i, state);
	    //Find an action card and play it
	    	if (card > adventurer && card <= treasure_map){
	    	    	cardNumToName(card, name);
	    	        playCard(card,1,1,1,state);
	    	        deckcount =  fullDeckCount(player, card, state);

	    	        count--;
	    	        isAction = 1;
	    	        break;
	    	    }
	    	}

	   	   //no action card to play
	    	if (isAction != 1){
	    	    count = 0;
	    	}
	  }
}




void playGame(int seed){

	int n;
	int numPlayers;
	int player;
	int success;
	int k[10];


	//set up how many random games to play
	for (n = 0; n < 10; n++) {

	newGame();
	struct gameState G;
	numPlayers = floor(Random() * MAX_PLAYERS); //make number of players random
	  if (numPlayers < 2){ 	numPlayers = 2;  }
	  else if (numPlayers > 4){	numPlayers= 4;  }

	  //generate random kingdom cards
	  selectKingdomCards(rand(),k);

	//Initialize Game
	success = initializeGame(numPlayers, k, seed, &G);

	if (success != 0) {
		printf("Initializing Game Error");
		numerrors++;
		exit(0);
	}


    	/*Give each player a turn*/
    	for(player = 0; player < numPlayers; player++){


    		//Action Phase
    	    G.phase = 0;
    	    testActionPhase(player, &G);

    		} //end for each player

	} // for loop for how many games

}


int main (int argc, char** argv)
{


	int seed;

	seed = rand();
	SelectStream(1);
	PutSeed(seed);

	playGame(seed);
	printf("All action phase tests ran successfully\n");
	printf("Number of errors found was %d\n", numerrors);

	return 0;
}

